/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier.gui;

import filecopier.Singleton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JProgressBar;
import javax.swing.Box;
import javax.swing.SwingWorker;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
 *
 * @author david
 */
public class ProgressBarPanel extends JPanel implements PropertyChangeListener {

    private JButton okBtn = null;
    private JButton cancelBtn = null;
    private JDialog dialog = null;
    private JLabel summary = null;
    private JTextArea textArea = null;
    private JProgressBar progressBar = null;

    public ProgressBarPanel(final String title) {
        super(new BorderLayout());
        this.add(getTopBox(), BorderLayout.NORTH);
        this.add(getBottomBox(), BorderLayout.CENTER);
        this.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setOpaque(true);

        // Create and display the dialog.
        dialog = new JDialog(Singleton.getInstance().getFrame(), title);
        dialog.setContentPane(this);
        dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        dialog.setIconImage(Singleton.getInstance().getImage16());
        dialog.pack();
        dialog.setVisible(true);
    }

    private JProgressBar getProgressBar() {
        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        return progressBar;
    }

    private javax.swing.JLabel getSummaryLabel() {
        summary = new javax.swing.JLabel();
        return summary;
    }

    private Box getTopBox() {
        Box topBox = Box.createVerticalBox();
        topBox.add(getProgressBar());
        topBox.add(getSummaryLabel());
        topBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        return topBox;
    }

    public JTextArea getTextArea() {
        if (textArea == null) {
            textArea = new JTextArea(10, 40);
            textArea.setEditable(false);
            textArea.setMargin(new java.awt.Insets(5, 5, 5, 5));
        }
        return textArea;
    }

    private JScrollPane getScrollPane() {
        JScrollPane scrollPane = new JScrollPane(getTextArea());
        return scrollPane;
    }

    private JButton getOKBtn() {
        okBtn = new JButton("OK");
        okBtn.setMnemonic(java.awt.event.KeyEvent.VK_O);
        okBtn.setEnabled(false);
        okBtn.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                dialog.dispose();
            }
        });
        return okBtn;
    }

    private JButton getCancelBtn() {
        cancelBtn = new JButton("Cancel");
        cancelBtn.setMnemonic(java.awt.event.KeyEvent.VK_C);
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                firePropertyChange("cancel", false, true);
            }
        });
        return cancelBtn;
    }

    private Box getBtnBox() {
        Box btnBox = Box.createHorizontalBox();
        btnBox.add(getOKBtn());
        btnBox.add(Box.createHorizontalStrut(20));
        btnBox.add(getCancelBtn());
        return btnBox;
    }

    private Box getBottomBox() {
        Box bottomBox = Box.createVerticalBox();
        bottomBox.add(getScrollPane());
        bottomBox.add(getBtnBox());
        return bottomBox;
    }

    public void propertyChange(PropertyChangeEvent pce) {
        if (pce.getPropertyName().equals("progress")) {
            // Set the new progress.
            int progress = (Integer) pce.getNewValue();
            progressBar.setValue(progress);
        } else if (pce.getPropertyName().equals("state") &&
                ((SwingWorker.StateValue) pce.getNewValue()).equals(SwingWorker.StateValue.DONE)) {
            // If the state of the SwingWorker is done, enable the ok button and disable the cancel button.
            okBtn.setEnabled(true);
            cancelBtn.setEnabled(false);

            // If the progressBar's value is 100, the operation completed. If not, it was cancelled.
            dialog.setTitle(progressBar.getValue() == 100 ? "Done!" : "Cancelled!");
        } else if (pce.getPropertyName().equals("summaryRpt")) {
            // The SwingWorker sends a summary report after it is finished.
            summary.setText((String) pce.getNewValue());
        }
    }
}
